package com.wyx.springbootkafkaconsumerone.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.config.SslConfigs;
import org.apache.kafka.common.security.auth.SecurityProtocol;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

/**
 * bsj kafka 配置
 *
 * @author wyx
 * @Date 2022/11/3
 */
//@EnableKafka
//@Configuration
public class BsjKafkaConfig {

  @Value("${spring.kafka.sasl_jaas_config}")
  private String saslJaasConfig;

  @Value("${spring.kafka.sasl-mechanism:}")
  private String saslMechanism;

  @Value("${spring.kafka.truststore-filename:}")
  private String truststoreFilename;

  @Value("${spring.kafka.truststore-password:}")
  private String truststorePassword;

  @Value("${spring.kafka.bootstrap-servers:}")
  private String bootstrapServers;

  @Value("${spring.kafka.consumer.group-id:}")
  private String groupId;

  @Bean
  public ConcurrentKafkaListenerContainerFactory<Integer, String> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<Integer, String> factory = new ConcurrentKafkaListenerContainerFactory();
    factory.setConsumerFactory(consumerFactory());
    return factory;
  }

  @Bean
  public ConsumerFactory<Integer, String> consumerFactory() {
    return new DefaultKafkaConsumerFactory(consumerConfigs());
  }

  @Bean
  public Map<String, Object> consumerConfigs() {
    Map<String, Object> map = new ConcurrentHashMap();
    map.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    map.put(ConsumerConfig.RECONNECT_BACKOFF_MS_CONFIG, 1000);
    map.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
    map.put(ConsumerConfig.RECONNECT_BACKOFF_MAX_MS_CONFIG, 5000);
    map.put(ConsumerConfig.RETRY_BACKOFF_MS_CONFIG, 1000);
    map.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    map.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    map.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
    map.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
    map.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
    map.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, SecurityProtocol.SASL_SSL.toString());
    map.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "");
    map.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, truststoreFilename);
    map.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, truststorePassword);
    map.put(SaslConfigs.SASL_JAAS_CONFIG, saslJaasConfig);
    map.put(SaslConfigs.SASL_MECHANISM, saslMechanism);
    return map;
  }

  @Bean
  public ProducerFactory<String, String> producerFactory() {
    return new DefaultKafkaProducerFactory<>(producerConfigs());
  }


  @Bean
  public Map<String, Object> producerConfigs() {
    Map<String, Object> props = new ConcurrentHashMap();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    props.put(ProducerConfig.RECONNECT_BACKOFF_MS_CONFIG, 1000);
    props.put(ProducerConfig.RECONNECT_BACKOFF_MAX_MS_CONFIG, 5000);
    props.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 1000);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, SecurityProtocol.SASL_SSL.toString());
    props.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "");
    props.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, truststoreFilename);
    props.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, truststorePassword);
    props.put(SaslConfigs.SASL_JAAS_CONFIG, saslJaasConfig);
    props.put(SaslConfigs.SASL_MECHANISM, saslMechanism);
    return props;
  }

  @Bean
  public KafkaTemplate<String, String> kafkaTemplate() {
    return new KafkaTemplate(producerFactory());
  }
}
